package org.grow.jpago.controller;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.AllArgsConstructor;
import org.grow.jpago.dao.DealBillDao;
import org.grow.jpago.dao.PersonDao;
import org.grow.jpago.dao.StudentScoreDao;
import org.grow.jpago.entity.*;
import org.grow.jpago.service.DealBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 21-7-14
 */

@RestController
@RequestMapping("/studentScore")
public class StudentScoreController {
// ant maven gradle
    @Autowired
    private StudentScoreDao studentScoreDao;
    @PutMapping("/")
    public Boolean insert(@RequestBody StudentScore studentScore){
        studentScore.setScoreId(0);
        studentScore.setVersion(0);
        StudentScore save = studentScoreDao.save(studentScore);


        return save.getScoreId()!=0;
    }

    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    QStudentScore s = QStudentScore.studentScore;

    @PostMapping("/query")
    public PagedResults<StudentScore> query(
            @RequestBody ScorePagedCondition scorePagedCondition){
        Integer pageNum = scorePagedCondition.getPageNum();
        Integer pageSize = scorePagedCondition.getPageSize();
        ScoreQueryCondition qc = scorePagedCondition.getQueryCondition();
        Integer pageStart = (pageNum-1)*pageSize;
//        limit start ,size
        BooleanBuilder builder = new BooleanBuilder();

        /*
        *     private String code;

    private String similarName;

    private Integer maxOnline;

    private Integer minOnline;*/
        if (StringUtils.hasText(qc.getCode())){
            builder.and(s.code.eq(qc.getCode()));
        }
        if (StringUtils.hasText(qc.getSimilarName())){
            builder.and(s.name.like("%"+qc.getSimilarName()+"%"));
        }
        if (qc.getMaxOnline()!=null && qc.getMaxOnline()!=0 ){
            builder.and(s.onlineScore.loe(qc.getMaxOnline()));
        }
        if (qc.getMinOnline()!=null && qc.getMinOnline()!=0){
            builder.and(s.onlineScore.goe(qc.getMinOnline()));
        }
        QueryResults<StudentScore> fetchResults = jpaQueryFactory.selectFrom(s)
                .where(builder)
                .orderBy(s.onlineScore.desc())
                .offset(pageStart)
                .limit(pageSize)
                .fetchResults();
//        PagedResults<StudentScore> pagedResults = new PagedResults<>();
//        pagedResults.setTotal(fetchResults.getTotal());
//        pagedResults.setResults(fetchResults.getResults());
//
//        return pagedResults;
        return new PagedResults<>(fetchResults.getTotal(),fetchResults.getResults());
    }

    @Autowired
    private DealBillDao dealBillDao;
    @Autowired
    private DealBillService dealBillService;
    @PostMapping("/deal")
    public Boolean deal(@RequestBody DealBill dealBill){
        dealBill.setDealBillId(0);
        dealBillDao.save(dealBill);
        System.out.println("交易信息：" +dealBill);
        String consumerName = dealBill.getConsumerName();
        String supplierName = dealBill.getSupplierName();
        Integer dealScore = dealBill.getDealScore();
        StudentScore consumer = studentScoreDao.findByName(consumerName);
        StudentScore supplier = studentScoreDao.findByName(supplierName);
        System.out.println("查询到交易双方："+consumer);
        System.out.println("查询到交易双方："+supplier);
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return dealBillService.txDeal(supplier,consumer,dealScore);

//        supplier.setOnlineScore(supplier.getOnlineScore()-dealScore);
//        studentScoreDao.save(supplier);
//
//        consumer.setOnlineScore(consumer.getOnlineScore()+dealScore);
//        studentScoreDao.save(consumer);




    }

}
